import config from '@/common/config'
const APPID = config.appId;
const WX_AUTH_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID +
  '&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=0#wechat_redirect';
export function getWxCode() {
  return new Promise((resolve, reject) => {
    let code = getUrlParam('code');
    if (code) {
      resolve(code);
      return;
    }
    let currentUrl = encodeURIComponent(window.location.href);
    window.location.replace(WX_AUTH_URL.replace('REDIRECT_URI', currentUrl));
  });

}

function getUrlParam(key) { //获取当前页面url中的参数
  var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
  var result = window.location.search.substr(1).match(reg);
  return result ? decodeURIComponent(result[2]) : '';
}
